/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.articleAdmin;

import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.entity.ArticleCategory;
import com.mxg.blog.service.ArticleCategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/5/13 0:08
 * 返回文章分类选项
 */
@CrossOrigin
@Controller
public class ArticleCategoryController {
    CqLogger logger = CqLogger.getInstance();

    private final ArticleCategoryService articleCategoryService;

    public ArticleCategoryController(ArticleCategoryService articleCategoryService)
    {
        this.articleCategoryService = articleCategoryService;
    }

    @PostMapping("api/getArticleCategory")
    public ResponseEntity<Map<String, Object>> getArticleCategory(HttpServletRequest request){
        // 设置返回数据
        Map<String, Object> resultMap = new HashMap<>();

        // 文章分类表
        List<ArticleCategory> articleCategoryList = articleCategoryService.getArticleCategory();

        // 提取文章分类的标题
        List<String> articleCategoryNameList = new ArrayList<>();
        for(ArticleCategory ac : articleCategoryList){
            articleCategoryNameList.add(ac.getArticleCategoryName());
        }

        logger.print(articleCategoryNameList);

        // 把articleCategoryNameList 存入 session
        HttpSession session = request.getSession(true);
        // 保存articleCategoryNameList
        session.setAttribute("cateName", articleCategoryNameList);

        // 返回所有的文章分类的标题
        resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
        resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        resultMap.put(ResultCode.data, articleCategoryNameList);

        return ResponseEntity.ok(resultMap);
    }


}
